using Gameplay.PVE.Utils;
using UnityEngine;
using Yoozoo.Managers;

namespace Gameplay.PVE.TurnBase
{
    public class DamageElement : ElementBase
    {
        public override void Execute()
        {
            base.Execute();

            int outputType = GetValue("OutputType");
            int output = GetOutput(outputType);
            bool isCrit = false;

            if (outputType == 1 && source.Data.critChance > 0)
            {
                //判断暴击
                if (Random.Range(0, 1f) < source.Data.critChance)
                {
                    isCrit = true;
                    output = (int)(output * source.Data.critTimes);
                }
            }
            
            target.Data.CurrentHp += output;
            target.Data.CurrentMp += 40;
            PassiveTriggerArg arg = ClassManager.Get<PassiveTriggerArg>();
            arg.source = source;
            source.Data.CurrentMp += 50;
            if (config.id != 800015)
            {
                target.Transmit(ETransmitType.GetHurt,arg);
            }
            target.Transmit(ETransmitType.ShowHit);
            ClassManager.Free(arg);
            source.Transmit(ETransmitType.CauseHurt);

            if (isCrit || outputType == 2)
            {
                TurnBaseManager.Instance.Shake(0.2f);
            }
            
            //飘字
            string content = string.Empty;
            ETextColor color = ETextColor.Yellow;
            if (outputType == 1)
            {
                if (!isCrit)
                {
                    content = (-output).ToString();
                    color = ETextColor.White;
                }
                else
                {
                    content = "爆头" + (-output).ToString();
                    color = ETextColor.Red;
                }
            }
            else if (outputType == 2)
            {
                content = (-output).ToString();
                if (config.flyText != null)
                {
                    content = config.flyText + content;
                }
                color = ETextColor.Yellow;
            }
            else if (outputType == 3)
            {
                content = "+" + output;
                color = ETextColor.Green;
            }

            var position = target.Data.position +
                           PveUtils.globalRatio * new Vector3(Random.Range(-0.3f,0.3f), Random.Range(-0.2f,0.7f), Random.Range(-0.3f,0.3f)) + new Vector3(0,PveUtils.globalRatio,0);
            PveEffectManager.Instance.PlayEffect(1,position,target.Data.forward,0.3f);
            
            //PveFlyTextManager.Instance.ShowFlyText(content,position + new Vector3(0,0.1f,0),0.8f,0.3f,color);

            IsFree = true;
        }

        private int GetOutput(int outputType)
        {
            float ratio = source.Data.AttackValue / target.Data.DefenceValue;
            if (source.Data.GetCounterType(target.Data.config.property) == ECounterType.Counter && outputType != 3)
            {
                ratio *= 2;
            }

            
            if (outputType == 1 || outputType == 2)
            {
                ratio *= -1;
            }

            return (int)(config.output * source.Data.damageRatio * ratio);
        }
    }
}